package com.hellogood.yaohuo.client.common;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Form;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;

public class SearchResultAsynCallback implements AsyncCallback<List<Form>> {
	private ListGrid listGrid;

	public SearchResultAsynCallback(ListGrid listGrid) {
		this.listGrid = listGrid;
	}

	@Override
	public void onFailure(Throwable caught) {
		SC.say(caught.getMessage());
	}

	@Override
	public void onSuccess(List<Form> result) {
		if (result != null) {
			if (result.size() == 0) {
				listGrid.setData(new Record[0]);
				SC.say(CommonMessages.NoData);
			} else {
				Record[] rds = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					String status = result.get(i).getStatus();
					if (status == null || status.equals("0")) {
						status = Constants.HUIZONG_NO;
					} else {
						status = Constants.HUIZONG_YES;
					}
					r.setAttribute("code", result.get(i).getBillNo());
					r.setAttribute("org", result.get(i).getOrgCode() + "-" + result.get(i).getOrgName());
					r.setAttribute("shop", result.get(i).getShopCode() + "-" + result.get(i).getShopName());
					r.setAttribute("pszx", result.get(i).getOrgCode() + "-" + result.get(i).getOrgName());
					r.setAttribute("vendor", result.get(i).getVendorCode() + "-" + result.get(i).getVendorName());
					r.setAttribute("date", result.get(i).getBillDate());
					r.setAttribute("amount", result.get(i).getCounts());
					System.out.println("***************************" + result.get(i).getCost());
					if (result.get(i).getCost() != null) {
						r.setAttribute("cost", result.get(i).getCost().toString());
					}
					if (result.get(i).getPsCost() != null) {
						r.setAttribute("PsCost", result.get(i).getPsCost().toString());
					}
					r.setAttribute("zdUser", result.get(i).getStaffCode() + "-" + result.get(i).getStaffName());
					r.setAttribute("zdDate", result.get(i).getBillDate());
					r.setAttribute("rzDate", result.get(i).getRzDate());
					r.setAttribute("status", status);
					r.setAttribute("remark", result.get(i).getRemark());
					rds[i] = r;
				}

				listGrid.setData(rds);
			}
		}
	}
}
